#app/models/user.py
from app import db
from datetime import datetime
import hashlib
class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.BigInteger, primary_key=True)
    openid = db.Column(db.String(80), unique=True, nullable=False)
    role = db.Column(db.String(20), default='user')  # user, admin, crawler
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

# from app import db
# from datetime import datetime


class Admin(db.Model):
    __tablename__ = 'admin'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)
    is_active = db.Column(db.Boolean, default=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    last_login = db.Column(db.DateTime)

    @staticmethod
    def hash_password(password):
        return hashlib.sha256(password.encode()).hexdigest()

    def verify_password(self, password):
        return self.password_hash == self.hash_password(password)

    @classmethod
    def create_admin(cls, username, password):
        admin = cls(
            username=username,
            password_hash=cls.hash_password(password)
        )
        db.session.add(admin)
        db.session.commit()
        return admin